1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.tapestry5.internal.services.compatibility;
16
17 import org.apache.tapestry5.ComponentResources;
18 import org.apache.tapestry5.alerts.AlertManager;
19 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
20 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
21 import org.apache.tapestry5.services.ComponentClasses;
22 import org.apache.tapestry5.services.ComponentMessages;
23 import org.apache.tapestry5.services.ComponentTemplates;
24 import org.apache.tapestry5.services.InvalidationEventHub;
25 import org.apache.tapestry5.services.compatibility.DeprecationWarning;
26 import org.slf4j.Logger;
27
28 import java.util.Map;
29
30 public class DeprecationWarningImpl implements DeprecationWarning
31 {
32 private final Logger logger;
33
34 private final AlertManager alertManager;
35
36 static class ParameterDeprecationKey
37 {
38 final String completeId, parameterName;
39
40 ParameterDeprecationKey(String completeId, String parameterName)
41 {
42 this.completeId = completeId;
43 this.parameterName = parameterName;
44 }
45
46 @Override
47 public boolean equals(Object o)
48 {
49 if (this == o) return true;
50 if (o == null || getClass() != o.getClass()) return false;
51
52 ParameterDeprecationKey that = (ParameterDeprecationKey) o;
53
54 if (!completeId.equals(that.completeId)) return false;
55 if (!parameterName.equals(that.parameterName)) return false;
56
57 return true;
58 }
59
60 @Override
61 public int hashCode()
62 {
63 int result = completeId.hashCode();
64 result = 31 * result + parameterName.hashCode();
65 return result;
66 }
67 }
68
69 static class ParameterValueDeprecationKey
70 {
71 final String completeId, parameterName;
72 final Object value;
73
74 ParameterValueDeprecationKey(String completeId, String parameterName, Object value)
75 {
76 this.completeId = completeId;
77 this.parameterName = parameterName;
78 this.value = value;
79 }
80
81 @Override
82 public boolean equals(Object o)
83 {
84 if (this == o) return true;
85 if (o == null || getClass() != o.getClass()) return false;
86
87 ParameterValueDeprecationKey that = (ParameterValueDeprecationKey) o;
88
89 if (!completeId.equals(that.completeId)) return false;
90 if (!parameterName.equals(that.parameterName)) return false;
91 if (value != null ? !value.equals(that.value) : that.value != null) return false;
92
93 return true;
94 }
95
96 @Override
97 public int hashCode()
98 {
99 int result = completeId.hashCode();
100 result = 31 * result + parameterName.hashCode();
101 result = 31 * result + (value != null ? value.hashCode() : 0);
102 return result;
103 }
104 }
105
106
107 private final Map<Object, Boolean> deprecations = CollectionFactory.newConcurrentMap();
108
109 public DeprecationWarningImpl(Logger logger, AlertManager alertManager)
110 {
111 this.logger = logger;
112 this.alertManager = alertManager;
113 }
114
115 public void componentParameter(ComponentResources resources, String parameterName, String message)
116 {
117 assert resources != null;
118 assert InternalUtils.isNonBlank(parameterName);
119 assert InternalUtils.isNonBlank(message);
120
121 ParameterDeprecationKey key = new ParameterDeprecationKey(resources.getCompleteId(), parameterName);
122
123 if (deprecations.containsKey(key))
124 {
125 return;
126 }
127
128 deprecations.put(key, true);
129
130 logMessage(resources, parameterName, message);
131 }
132
133 public void ignoredComponentParameters(ComponentResources resources, String... parameterNames)
134 {
135 assert resources != null;
136
137 for (String name : parameterNames)
138 {
139
140 if (resources.isBound(name))
141 {
142 componentParameter(resources, name, "This parameter is ignored and may be removed in a future release.");
143 }
144 }
145 }
146
147 public void componentParameterValue(ComponentResources resources, String parameterName, Object parameterValue, String message)
148 {
149 assert resources != null;
150 assert InternalUtils.isNonBlank(parameterName);
151 assert InternalUtils.isNonBlank(message);
152
153 ParameterValueDeprecationKey key = new ParameterValueDeprecationKey(resources.getCompleteId(), parameterName, parameterValue);
154
155 if (deprecations.containsKey(key))
156 {
157 return;
158 }
159
160 deprecations.put(key, true);
161
162 logMessage(resources, parameterName, message);
163 }
164
165 private void logMessage(ComponentResources resources, String parameterName, String message)
166 {
167 String text = String.format("Component %s, parameter %s: %s\n(at %s)",
168 resources.getCompleteId(),
169 parameterName,
170 message,
171 resources.getLocation());
172
173 logger.error(text);
174
175 alertManager.warn(text);
176 }
177
178 public void setupClearDeprecationsWhenInvalidated(
179 @ComponentClasses
180 InvalidationEventHub componentClassesHub,
181 @ComponentMessages
182 InvalidationEventHub messagesHub,
183 @ComponentTemplates
184 InvalidationEventHub templatesHub)
185 {
186 componentClassesHub.clearOnInvalidation(deprecations);
187 messagesHub.clearOnInvalidation(deprecations);
188 templatesHub.clearOnInvalidation(deprecations);
189 }
190
191
192 }